class SqlBuilder():
    '''SQL builder for combine SQL more easily'''

    def __init__(self, base_sql):
        '''

        :param base_sql:  select ... from ...
        '''
        self.is_append_where = False
        self.base_sql = base_sql
        self.sql = base_sql

    def build(self) -> str:
        '''

        :return: builded sql string
        '''
        self.sql += ';'
        return self.sql

    def build_with_paging(self, page, record_per_page=50):
        self.sql += ' LITMIT {},{};'.format(page * record_per_page, record_per_page)
        return self.sql

    def append_and_sql(self, append_sql):
        if self.is_append_where:
            self.sql += ' AND ' + append_sql
        else:
            self.is_append_where = True
            self.sql += ' WHERE ' + append_sql

    def append_or_sql(self, append_sql):
        if self.is_append_where:
            self.sql += ' OR ' + append_sql
        else:
            self.is_append_where = True
            self.sql += ' WHERE ' + append_sql

    def append_and_in_criteria(self, field, criteria_list):
        if criteria_list:
            if self.is_append_where:
                self.sql += ' AND {} IN ('.format(field)
            first_criteria = True
            for criteria in criteria_list:
                if isinstance(criteria, str):
                    if first_criteria:
                        self.sql += '\'{}\''.format(criteria)
                    else:
                        self.sql += ',\'{}\''.format(criteria)
            self.sql += ')'

    def append_or_in_criteria(self, field, criteria_list):
        if criteria_list:
            if self.is_append_where:
                self.sql += ' OR {} IN ('.format(field)
            first_criteria = True
            for criteria in criteria_list:
                if isinstance(criteria, str):
                    if first_criteria:
                        first_criteria = False
                        self.sql += '\'{}\''.format(criteria)
                    else:
                        self.sql += ',\'{}\''.format(criteria)
            self.sql += ')'
